import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad

IV_STRING = "16-Bytes--String"


def decrypt_aes(content: str) -> str:
    """
    AES解密函数，与Java中的decryptAES方法功能一致
    :param content: 待解密的Base64编码字符串
    :param key: 解密密钥
    :return: 解密后的字符串
    """
    try:
        # Base64解码
        encrypted_bytes = base64.b64decode(content)
        # 密钥处理
        en_code_format = get_key_by_pass().encode('utf-8')
        # 创建AES加密器
        cipher = AES.new(en_code_format, AES.MODE_CBC,
                         IV_STRING.encode('utf-8'))
        # 解密并去除填充
        decrypted_bytes = unpad(cipher.decrypt(
            encrypted_bytes), AES.block_size)
        # 转换为UTF-8字符串
        return decrypted_bytes.decode('utf-8')
    except Exception as e:
        print(f"解密错误: {str(e)}")
        return None


def get_key_by_pass() -> str:
    """
    生成密钥，与Java中的getKeyByPass方法功能一致
    :return: 16位密钥字符串
    """
    # 生成秘钥
    key_bytes = ("cn.com.dyg.work" + "dygapp").encode('utf-8')
    key = key_bytes.hex()
    # 确保密钥长度为16字节
    if len(key) >= 16:
        return key[:16]
    else:
        return "16-Bytes--String"


if __name__ == "__main__":
    # 示例用法
    encrypted_content = ""
    decrypted_str = decrypt_aes(encrypted_content)
    print(decrypted_str)
